//
//  FilterView.m
//  NOAHWM
//
//  Created by Ryan on 13-6-13.
//  Copyright (c) 2013年 Ryan. All rights reserved.
//

#import "FilterView.h"
#import <QuartzCore/QuartzCore.h>

#define kPopOriginY 45.f

@implementation FilterView

@synthesize filterTableView;
@synthesize filterProperties;
@synthesize type,locationType;

- (void)setLocationType:(FilterLocationType)lt
{
    locationType = lt;
    if(locationType == kLocationCenter)
    {
        self.filterTableView.rowHeight = 30.f;
        CGRect rect = self.filterTableView.frame;
        rect.origin.y = kPopOriginY;
        rect.origin.x = ([UIScreen mainScreen].bounds.size.width-100.f)/2;
        rect.size.width = 100.f;
        self.filterTableView.frame = rect;
    }
    else if(locationType == kLocationRight)
    {
        self.filterTableView.rowHeight = 45.f;
        CGRect rect = self.filterTableView.frame;
        rect.origin.y = kPopOriginY;
        rect.origin.x = [UIScreen mainScreen].bounds.size.width-45.f;
        rect.size.width = 45.f;
        self.filterTableView.frame = rect;
    }
    else
    {
        
    }
}

#pragma mark - Public methods

- (void)reloadData
{
    [self.filterTableView reloadData];
}

#pragma mark - UIView methods
- (void)awakeFromNib
{
    [super awakeFromNib];
    self.filterTableView.layer.shadowOffset = CGSizeMake(-2, 2);
    self.filterTableView.layer.shadowOpacity = 0.5f;
    self.filterTableView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.filterTableView.clipsToBounds = NO;
}

- (void)dealloc
{
    [filterProperties release];
    [filterTableView release];
    [super dealloc];
}

#pragma mark - UITableViewDataSource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.filterProperties.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Call_normal@2x.png Call_highlighted@2x.png
    if(self.locationType == kLocationRight)
    {
        static NSString *cellIdentify = @"cellIdentify";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
        if(nil == cell)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify] autorelease];
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.rowHeight)];
            imgView.tag = 100;
            [cell.contentView addSubview:imgView];
            [imgView release];
        }
        UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:100];
        NSString *imageName = [self.filterProperties objectAtIndex:indexPath.row];
        NSString *normalImageName = [NSString stringWithFormat:@"%@_normal@2x.png",imageName];
        NSString *highlightedImageName = [NSString stringWithFormat:@"%@_highlighted@2x.png",imageName];
        imgView.image = [UIImage imageNamed:normalImageName];
        imgView.highlightedImage = [UIImage imageNamed:highlightedImageName];
        return cell;
    }
    else if(self.locationType == kLocationCenter)
    {
        static NSString *locationCellIdentify = @"locationCellIdentify";
        UITableViewCell *locationCell = [tableView dequeueReusableCellWithIdentifier:locationCellIdentify];
        if(nil == locationCell)
        {
            locationCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:locationCellIdentify] autorelease];
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.rowHeight)];
            imgView.tag = 100;
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10.f, 0, tableView.frame.size.width-20.f, tableView.rowHeight)];
            label.tag = 101;
            label.font = [UIFont boldSystemFontOfSize:14.f];
            label.textAlignment = UITextAlignmentCenter;
            label.minimumFontSize = 10.f;
            label.textColor = [UIColor whiteColor];
            label.backgroundColor = [UIColor clearColor];
            [locationCell.contentView addSubview:imgView];
            [locationCell.contentView addSubview:label];
            [imgView release];
            [label release];
        }
        UIImageView *imgView = (UIImageView *)[locationCell.contentView viewWithTag:100];
        UILabel *label = (UILabel *)[locationCell.contentView viewWithTag:101];
        imgView.image = [UIImage imageNamed:@"bg_pop_normal@2x.png"];
        imgView.highlightedImage = [UIImage imageNamed:@"bg_pop_highlighted@2x.png"];
        label.text = [self.filterProperties objectAtIndex:indexPath.row];
        return locationCell;
    }
    else
        return nil;
}

#pragma mark - UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(self.locationType == kLocationRight)
    {
        switch (type) {
            case kProDetailFilter:
                if(indexPath.row == 0)
                    [[NSNotificationCenter defaultCenter] postNotificationName:kShowPhoneCallNotification object:nil];
                else
                    [[NSNotificationCenter defaultCenter] postNotificationName:kCollectProductNotification object:nil];
                break;
            case kProYuyueFilter:
                [[NSNotificationCenter defaultCenter] postNotificationName:kShowPhoneCallNotification object:nil];
                break;
            case kProYuyueListRightFilter:
                [[NSNotificationCenter defaultCenter] postNotificationName:kShowPhoneCallNotification object:nil];
                break;
            case kProYuyueCancel:
                [[NSNotificationCenter defaultCenter] postNotificationName:kShowPhoneCallNotification object:nil];
            case kAccountProductDetailFilter:
                [[NSNotificationCenter defaultCenter] postNotificationName:kShowPhoneCallNotification object:nil];
            case kDynamicDetailFilter:
                [[NSNotificationCenter defaultCenter] postNotificationName:kShowPhoneCallNotification object:nil];
            default:
                break;
        }
    }
    else if(self.locationType == kLocationCenter)
    {
        switch (type) {
            case KProYuyueListFilter:
                [[NSNotificationCenter defaultCenter] postNotificationName:kFilterProYuyueListNotification object:[NSNumber numberWithInteger:indexPath.row]];
                break;
            default:
                break;
        }
    }
    else
    {
        
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:kHideFilterViewNotification object:nil userInfo:nil];
}

#pragma mark - UIResponder methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[NSNotificationCenter defaultCenter] postNotificationName:kHideFilterViewNotification object:nil userInfo:nil];
}

@end
